<?php 
// +----------------------------------------------------------------------
// | RXThinkCMF框架 [ RXThinkCMF ]
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.rxthink.cn
// +----------------------------------------------------------------------
// | Author: 牧羊人 <1175401194@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;


use app\admin\service\OfCopyrightService;
use app\common\controller\Backend;
use think\facade\View;
use think\facade\Db;

/**
 * 著作权管理-控制器
 * @author 牧羊人
 * @since: 2021/12/21
 * Class Ofcopyright
 * @package app\admin\controller
 */
class Ofcopyright extends Backend
{
    /**
     * 初始化方法
     * @author 牧羊人
     * @since: 2021/12/21
     */
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->model = new \app\admin\model\OfCopyright();
        $this->service = new OfcopyrightService();
        $this->OfcompanyService = new \app\admin\service\OfCompanyService();
    }
	
    /**
     * 数据列表页
     * @return mixed
     * @since 2020/7/5
     * @author 牧羊人
     */
    public function index()
    {
        $companyList = '';
        $contractList = '';
        $company_id = input("company_id",0);
        $wh = [];
        $wh[] = ['mark','=',1];
        if(empty($company_id)){
            // 客户列表
            $companyMod = new \app\admin\model\OfCompany();
            $companyList = $companyMod->where($wh)->order("id desc")->select()->toArray();
        }else{
            $wh[] = ['company_id','=',$company_id];
        } 


        //将状态 类型改为config0配置 多处使用便于维护
        $copyright_speed = config('admin.copyright_speed');
        $copyright_type = config('admin.copyright_type');
        if (IS_POST) {
            $result = $this->service->getList();
            foreach ($result['data'] as $key => &$value) {
                $result['data'][$key]['speed']     = $copyright_speed[$value['speed']];
                $result['data'][$key]['type']      = $copyright_type[$value['type']];
            }
            return $result;
        }  
        View::assign("copyright_speed", $copyright_speed);
        View::assign("copyright_type", $copyright_type);

        View::assign("company_id", $company_id);
        View::assign("companyList", $companyList);
        // 合同列表
        $contractMod = new \app\admin\model\OfContract();
        $contractList = $contractMod->where($wh)->order("id desc")->select()->toArray();
        View::assign("contractList", $contractList);
        return parent::index(); // TODO: Change the autogenerated stub
    }

    /**
     * 添加或编辑
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author 牧羊人
     * @since 2020/7/2
     */
    public function edit()
    {
        if (IS_POST) {
            $company_id = input('company_id','');
            if(!$company_id){
                return message('请选择正确的客户',false);
            }
            $result = $this->service->edit();
            return $result;
        }
        // 客户列表
        $companyMod = new \app\admin\model\OfCompany();
        $list = $companyMod->where("mark", 1)->order("id desc")->select()->toArray();
        View::assign("list", $list);

        $info = [];
        $id = input("get.id", 0);
        $contractInfo = "";
        if ($id) {
            $info = $this->model->getInfo($id);
            if(!empty($info['contract_id'])){
                $contractInfo = Db::name("of_contract")->where("id",$info['contract_id'])->find();
            }
        } else {
            $data = func_get_args();
            if (isset($data[0])) {
                foreach ($data[0] as $key => $val) {
                    $info[$key] = $val;
                }
            }
        }
        View::assign("contractInfo", $contractInfo);
        View::assign("info", $info);

        //将状态 类型改为config0配置 多处使用便于维护
        $copyright_speed = config('admin.copyright_speed');
        $copyright_type = config('admin.copyright_type');
        View::assign("copyright_speed", $copyright_speed);
        View::assign("copyright_type", $copyright_type);
        return $this->render();
    }


    /**
     * 弹窗添加 通知书
     * @return mixed
     * @since 2020/7/5
     * @author 牧羊人
     */
    public function add_notice()
    {   
        $param = request()->param();

        View::assign("id", $param['id']);
        return $this->render();
    }


    public function download()
    {
        ini_set('max_execution_time', '0');
        $param = request()->param();
        // 查询要导出的数据
        $map = [
            ['mark', '=', 1],
        ];
        
        if ($param['case_name']) {
            $map[] = ['case_name', 'like', '%' . $param['case_name'] . '%'];
        }
        if ($param['speed'] != '') {
            $map[] = ['speed', '=', $param['speed']];
        }
        if ($param['company_id']) {
            $map[] = ['company_id', '=', $param['company_id']];
        }else{            
            $admin_id = session("adminId");
            if($admin_id != 1){         
                $wh_or = [];
                $wh_or[] = ["service_id","=",$admin_id];
                $wh_or[] = ["agent_id","=",$admin_id];
                $wh_or[] = ["peer_id","=",$admin_id];
                $arr_ids = Db::name("of_company")->where('mark',1)->where(function($query) use($wh_or){$query->whereOr($wh_or);})->column("id");
                if(!empty($arr_ids)){
                    $map[] = ['company_id', 'in', $arr_ids];            
                }
            }
        }
        $head = [
            ['title' => '客户名称', 'width' => 50],
            ['title' => '合同编号', 'width' => 20],
            ['title' => '案件名称', 'width' => 50],
            ['title' => '案件编号', 'width' => 20],
            ['title' => '证书编号', 'width' => 20],
            ['title' => '申请日期', 'width' => 15],
            ['title' => '注册日期', 'width' => 15],
            ['title' => '案件进度', 'width' => 15],
            ['title' => '业务类型', 'width' => 15],
            ['title' => '备注', 'width' => 30],
        ];

        $data = $this->model->where($map)->order('id desc')->select();

        $new_data = [];
        foreach ($data as $key => $v) {
            $tmp = [];
            //客户名称 名称获取
            $name = Db::name("of_company")->where("id", $v['company_id'])->value('name');
            //合同编号
            $contract_no = Db::name("of_contract")->where("id", $v['contract_id'])->value('contract_no');
            //案件进度
            $speedlist = config('admin.copyright_speed');
            $speed     = $speedlist[$v['speed']];
            //案件状态
            $typeList = config('admin.copyright_type');
            $type     = $typeList[$v['type']];

            $tmp[] = $name;
            $tmp[] = $contract_no;
            $tmp[] = $v['case_name'];
            $tmp[] = $v['case_number'];
            $tmp[] = $v['certificate_number'];
            $tmp[] = $v['apply_date'];
            $tmp[] = $v['register_date'];
            $tmp[] = $speed;
            $tmp[] = $type;
            $tmp[] = $v['remark'];

            $new_data[] = $tmp;
        }
        $fileName = '著作权(' . date("Y-m-d H:i:s") . ')';
        $this->OfcompanyService->exportExcel($head, $new_data, $fileName);
    }
}